package com.csy.admin.util;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csy.common.xss.SQLFilter;
import org.apache.commons.lang3.StringUtils;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 类名:查询参数
 * Created by CSY on 2018/9/17.
 */
public class Query<T> extends LinkedHashMap<String,Object> {
    private static final long serialVersionUID = 1L;
    /**
     * mybatis-plus分页参数
     */
    private Page<T> page;
    /**
     * 当前页码
     */
    private int currentPage = 1;
    /**
     * 每页条数
     */
    private int limit = 10;

    public Query(Map<String,Object> params){
        this.putAll(params);

        if(params.get("page") != null){
            currentPage = Integer.parseInt((String) params.get("page"));
        }
        if(params.get("limit") != null){
            limit = Integer.parseInt((String) params.get("limit"));
        }

        this.put("offset",(currentPage-1)*limit);
        this.put("page",currentPage);
        this.put("limit",limit);

        String sidx = SQLFilter.sqlInject((String) params.get("sidx"));
        String order = SQLFilter.sqlInject((String) params.get("order"));

        //mybatis-plus分页
        this.page = new Page<>(currentPage,limit);
        //排序
        if(StringUtils.isNotBlank(sidx) && StringUtils.isNotBlank(order)){
            if("ASC".equalsIgnoreCase(order)) {
                page.setAsc(sidx);
            }
        }
    }

    public Page<T> getPage() {
        return page;
    }

    public void setPage(Page<T> page) {
        this.page = page;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }
}
